I’ve finally improved my ray tracer so that It can now render the objects that have mirror like material properties and transparent objects that can refract light ( glass, water ). While doing so I’ve sacrificed so many brain cells but final results left some smile on my face and I guess it worth the effort.

Before the mirror-like and transparent objects, I was tracing one ray per pixel and whenever such rays hit an object in the scene a shadow ray was cast to find the color of the pixel. Due to the nature of the mirror-like and transparent objects this scheme was not valid anymore.

As you can see in the Figure-1 below, mirror-like objects reflect the incoming ray in the opposite direction of the incoming ray by the same incident angle. Transparent objects are more challenging since they both reflect and refract incoming rays according to Snell’s Law and Fresnel’s Law. In the end since the refractions and reflections are recursive phenomenon in nature we end up with too many rays messing around in our scene. Of course we need to limit recurrence of such phenomena in our ray tracer by a predefined recursion depth else render times may increase drastically or even we can overflow the stack easily :)

recursive_ray_tracing

Figure-1: Recursive ray tracing illustration with reflection and refraction phenomenon [1]

As I have mentioned, I have used Snell’s law for calculating refracted rays. I also used Fresnel’s law to calculate reflected and refracted rays when a ray hits a transparent object since objects with transparent material properties both reflect and refract rays. Finally I’ve implemented Beer’s law for calculating the loss of energy due to travelling inside a medium.

Here are some results from my ray tracer:

cornelbox_glass

Figure-2: Cornell box variant with a fully reflective and fully transparent sphere

BVH is constructed in 0.00 seconds
Rendered model cornellbox_glass.png in 0.65 seconds
horse_and_glass_mug

Figure-3: Scene with a horse, reflective ground and transparent mug

BVH is constructed in 0.05 seconds
Rendered model horse_and_glass_mug.png in 6.22 seconds
glass_plates

Figure-4: Scene with glass plates and a sphere

BVH is constructed in 0.00 seconds
Rendered model glass_plates.png in 2.10 seconds
killeroo_half_mirror

Figure-5: Killeroo model with half mirror-like material properties

BVH is constructed in 0.17 seconds
Rendered model killeroo_half_mirror.png in 3.06 seconds
killeroo_mirror

Figure-6: Killeroo model with full mirror-like material properties

BVH is constructed in 0.17 seconds
Rendered model killeroo_mirror.png in 3.10 seconds
killeroo_glass

Figure-7: Killeroo model with full transparent material properties

BVH is constructed in 0.16 seconds
Rendered model killeroo_glass.png in 7.06 seconds

For fun part, in this section you can see my failed attempts while implementing reflections and refractions.


References

1 - https://www.scratchapixel.com/images/upload/ray-tracing-refresher/rt-recursive.png?